﻿//xu ly phan trang cho coms, cho mua
document.observe("dom:loaded", initDetailCM)
function initDetailCM() {
    if ($("dChoMua") && $("spLoadingCM")) {
        if (parseInt($("dChoMua").getHeight()) > 10 && parseInt($("dChoMua").getWidth()) > 10) {
            var emLoading = null;
            if ($("spLoadingCM").select("em").length > 0) {
                emLoading = $("spLoadingCM").select("em")[0];
            }
            $("spLoadingCM").setStyle({
                height: (parseInt($("dChoMua").getHeight()) + 20) + 'px'
            });
            // set center for icon loading; 50 la chieu dai` (rong) hinh /2
            var topLoad = ((parseInt($("spLoadingCM").getHeight()) / 2) - 50);
            var leftLoad = ((parseInt($("spLoadingCM").getWidth()) / 2) - 50);
            $(emLoading).setStyle({
                top: topLoad + 'px',
                left: (Math.round(leftLoad) - 9) + 'px'
            });
        }
    }
}
//var  strDealID='3335';
function showWithPT(num, name) {
    var strUrlLocation = window.location.href;
    strUrlLocation = strUrlLocation.replace('tp-ho-chi-minh/', '').replace('ha-noi/', '');
    var strUrlLink = strPathLink_All.replace('tp-ho-chi-minh/', '').replace('ha-noi/', '');
    if (strUrlLocation.toLowerCase().indexOf("/yahoo/") != -1) {
        strUrlLink = strPathLink_All + "yahoo/";
    }
    if ($("spLoadingCM") && $("spLoadingCM").style.display == "none") {
        $("spLoadingCM").style.display = "block";
        FadeoutCM($("dChoMua"));
    }
    if (name == "coms") {
        //xy ly an hien div
        loadComs_ChoMua("iComments");
        // phan trang cho comments
        if (strUrlLocation.toLowerCase().indexOf("/yahoo/") != -1) {
            utils.call(strUrlLink + "nhommua/vip_detail.aspx", { "flag": "view", "id": strDealID, "curpage": num }, "POST", "showCommentsByDeal", "showCommentsByDeal");
        } else {
            utils.call(strUrlLink + strStateNM + "/nhommua/vip_detail.aspx", { "flag": "view", "id": strDealID, "curpage": num }, "POST", "showCommentsByDeal", "showCommentsByDeal");
        }
    }
    else {
        //xy ly an hien div
        //loadComs_ChoMua("iChoMua");
        // phan trang cho chomua
        if (strUrlLocation.toLowerCase().indexOf("/yahoo/") != -1) {
            utils.call(strUrlLink + "nhommua/vip_detail.aspx", { "flag": "viewChoMua", "id": strDealID, "curpage": num }, "POST", "showChoMuaByDeal", "showChoMuaByDeal");
        } else {
            utils.call(strUrlLink + strStateNM + "/nhommua/vip_detail.aspx", { "flag": "viewChoMua", "id": strDealID, "curpage": num }, "POST", "showChoMuaByDeal", "showChoMuaByDeal");
        }
    }
}
function showChoMuaByDeal(objChoMua) {
    var strHtml = "";
    try {
        var strUrlLocation = window.location.href;
        strUrlLocation = strUrlLocation.replace('tp-ho-chi-minh/', '').replace('ha-noi/', '');
        var strUrlLink = strPathLink_All.replace('tp-ho-chi-minh/', '').replace('ha-noi/', '');
        if (strUrlLocation.toLowerCase().indexOf("/yahoo/") != -1) {
            strUrlLink = strPathLink_All + "yahoo/";
        }
        if (objChoMua.dsChoMua.length > 0) {
            var iTotalPage = parseInt(objChoMua.dsChoMua[0].total);
            var iCountPage = parseInt(objChoMua.dsChoMua[0].iCountPage);
            var iCurrentPage = parseInt(objChoMua.dsChoMua[0].iCurrentPage);
            strHtml += "<div class='list-comments' id='listChoMua'>";
            for (var i = 0; i < objChoMua.dsChoMua.length; i++) {
                //tinh stt va set DuocMua
                var iSTT = ((iCurrentPage * iCountPage) - iCountPage + i + 1);
                clsWait = "comment-content";
                if (checkDaNhan(objChoMua.dsNhanHang, objChoMua.dsChoMua[i].idtk)) {
                    clsWait += " received";
                }
                else if (parseInt(objChoMua.dsChoMua[0].sldaban) <= parseInt(objChoMua.dsChoMua[0].sltoida)) {
                    // theo sl da ban
                    if (iSTT <= parseInt(objChoMua.dsChoMua[0].sldaban)) {
                        clsWait += " waiting";
                    }
                }
                else {
                    // theo sl max
                    if (iSTT <= parseInt(objChoMua.dsChoMua[0].sltoida)) {
                        clsWait += " waiting";
                    }
                }
                strHtml += "<em class='line-hor'></em>" +
                       "<div class='" + clsWait + "'>" +
                       "<span class='order-num'>" + iSTT + "</span>" +
                       "<div class='waiting-left'>" +
                                "<a href='" + strUrlLink + strStateNM + "/profile/comments.aspx?mid=" + objChoMua.dsChoMua[i].idtk + (strLangNM == "en" ? "?lang=en" : "") + "'><img src='" + objChoMua.dsChoMua[i].avatar + "' width='52' height='52' alt='" + objChoMua.dsChoMua[i].nickname + "'/></a>" +
                                "<p style='padding-top:6px;'><a href='" + strUrlLink + strStateNM + "/profile/comments.aspx?mid=" + objChoMua.dsChoMua[i].idtk + (strLangNM == "en" ? "?lang=en" : "") + "' name='' style=' float:none'>" + objChoMua.dsChoMua[i].nickname + "</a></p>" +
                                "<p>" + objChoMua.dsChoMua[i].orderdate + "</p>" +
                            "</div>" +
                        "</div>";
            }
            strHtml += "</div>";
            if (objChoMua.dsChoMua.length > 0) {
                //xet du lieu phan trang
                var strPaging = "";
                if (iTotalPage > 1) {
                    strPaging += "<div class='paging-comment'>";
                    if (iCurrentPage > 1) strPaging += "<a href='javascript:void(0);' onclick=showWithPT(" + (iCurrentPage - 1 <= 1 ? 1 : iCurrentPage - 1) + ",'chomua') name='' class='btn-paging'>&laquo;Previous</a>";
                    var iStart, iEnd = 0;
                    iStart = (iCurrentPage - 2) <= 1 ? 1 : iCurrentPage - 2;
                    iEnd = iStart + 4 >= iTotalPage ? iTotalPage : iStart + 4;
                    for (var i = iStart; i <= iEnd; i++) {
                        strPaging += (i == iCurrentPage) ? "<span>" + i + "</span>" : "<a href='javascript:void(0);' onclick=showWithPT(" + i + ",'chomua')>" + i + "</a>";
                    }
                    strPaging += ((iCurrentPage < iTotalPage) ? "<a href='javascript:void(0);' onclick=showWithPT(" + (iCurrentPage + 1 >= iTotalPage ? iTotalPage : iCurrentPage + 1) + ",'chomua')  name='' class='btn-paging'>Next&raquo;</a></div>" : "</div>");
                }
            }
            $("listChoMua").innerHTML = strHtml;
            $("dCMPagging").innerHTML = strPaging;
        }
        if ($("spLoadingCM") && $("spLoadingCM").style.display == "block") {
            FadeinCM($("dChoMua"));
            //FadeinCM($("spLoadingCM"));
            $("spLoadingCM").style.display = "none";
        }
    }
    catch (e)
    { }
}

function checkDaNhan(dsNhanHang, value) {
    for (var i = 0; i < dsNhanHang.length; i++) {
        if (dsNhanHang[i].MemberOrder == value)
            return true;
    }
    return false;
}
//function FadeoutCM(element) {
//    new Effect.Fade(element,
//       { duration: 0.5, from: 1.0, to: 0.5 });
//}
//function FadeinCM(element) {
//    new Effect.Appear(element,
//       { duration: 0.5, from: 0.5, to: 1.0 });
//}
function FadeinCM(element) {
    new Effect.Opacity(element,
       { duration: 0.5, from: 0.5, to: 1.0 });
}
function FadeoutCM(element) {
    new Effect.Opacity(element,
       { duration: 0.5, from: 1.0, to: 0.5 });
}